package com.example.uit_management;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.concurrent.Semaphore;

import org.w3c.dom.Text;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class Activity_Score extends Activity {
	
	/**Mã sinh viên */
	static final String Student_ID =MainActivity.Student_ID; 
	/**Pass word */
	static final String Student_ID_pass =MainActivity.Student_ID_pass; 
	
	// Variable of class
	
	/**Biến kiểu listView */
	private ListView list;
	/**Biến kiểu ArrayAdapterRegistry */
	private ArrayAdapterScore adapter;
	/**Biến kiểu JsonParser_SCoreStudent lấy thong tin ve diem cua sinh vien*/
	private JsonParser_ScoreStudent Json_scorestudent =new JsonParser_ScoreStudent();
	/**Biến kiểu JsonParser_regcourse lấy thong tin ve diem cua sinh vien*/
	private JsonParser_regCourse Json_regcourse =new JsonParser_regCourse();
	/**Biến kiểu ArrayList<HashMap<String, String>> lấy diem từng môn học ma sinh vien dang ki (Khong hoc lai)*/
	private ArrayList<HashMap<String, String>> array_MAP=new ArrayList<HashMap<String, String>>();
	/**Biến kiểu ArrayList<HashMap<String, String>> lấy diem từng môn học ma sinh vien dang ki(Ke ca mon hoc lai) */
	private ArrayList<HashMap<String, String>> array_MAP_full=new ArrayList<HashMap<String, String>>();
	/** Mang khoa hoc dang ki */
	ArrayList<String> ArrayList ;
	/** spinner */
	private Spinner spinner;	
	/** spinner ArrayAdapter*/
	private ArrayAdapter spinnerArrayAdapter;
	/** Nien khoa **/
	private static String Spinner_semester="Tất cả";
	/** chieu cao */	
	static int Height_sore=15;
	/** tong diem */
	float Total_score =0;
	/** Tong tinh chi */
	float Total_TCC_full = 0;
	/** Tong tinh chi da co diem */
	float Total_TCC = 0;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	Log.e(Activity_Score.class.toString(),"Socre 1.1");
        super.onCreate(savedInstanceState);
        Log.e(Activity_Score.class.toString(),"Socre 1.2");
        setContentView(R.layout.activity_score);
        Log.e(Activity_Score.class.toString(),"Socre 1.3");
        Menu_GridView.dialog.dismiss();
        try{        
        // Lay diem sinh vien da dang ki theo ma so sinh vien
        array_MAP=Json_scorestudent.getMap();      
     // Lay diem sinh vien da dang ki theo ma so sinh vien
        array_MAP_full=Json_regcourse.getMap_score();    
        // xap xep
        Log.e(Activity_Score.class.toString(),"Socre 3.2");
        Collections.sort(array_MAP, new ArrayMap_sore_semester());
        Collections.sort(array_MAP_full, new ArrayMap_sore_semester());
        Log.e(Activity_Score.class.toString(),"Socre 3.3");
        // tao spinner chon hoc ki hoc
        ArrayList = new ArrayList<String>();
        ArrayList.clear();
        ArrayList.add("Tất cả");
        HashMap<String, String> json_current;
        HashMap<String, String> json_before;
        Log.e(Activity_Score.class.toString(),"Socre 3");
        for (int i = 0; i < array_MAP_full.size(); i++) {
        	if(i == 0){
        		Log.e(Activity_Score.class.toString(),"Socre 3.1");

        		json_current = array_MAP_full.get(i);
        		ArrayList.add(json_current.get(JsonParser_ScoreStudent.KEY_semesterYear)+"_"+json_current.get(JsonParser_ScoreStudent.KEY_semesterIndex));
        	}else{    
        		Log.e(Activity_Score.class.toString(),"Socre 3.2");

	        	json_current =array_MAP_full.get(i);
	        	json_before =array_MAP_full.get(i-1);
	        	if(!((json_current.get(JsonParser_ScoreStudent.KEY_semesterYear)+json_current.get(JsonParser_ScoreStudent.KEY_semesterIndex)).equals(json_before.get(JsonParser_ScoreStudent.KEY_semesterYear)+json_before.get(JsonParser_ScoreStudent.KEY_semesterIndex)))){
	        		ArrayList.add(json_current.get(JsonParser_ScoreStudent.KEY_semesterYear)+"_"+json_current.get(JsonParser_ScoreStudent.KEY_semesterIndex));
        		}
        	}			
		}
        /////////////////////////////
        // Spriner
        ///////////////////////////
        // set semetester for spinner
        Spinner_semester="Tất cả";
        spinner = (Spinner) findViewById(R.id.spinner);
        spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,ArrayList);
        spinner.setAdapter(spinnerArrayAdapter);
        spinner.setSelection(ArrayList.size()-1);
        // set lang nghe sellectItem
        spinner.setOnItemSelectedListener(Spinner_ItemSelectedListener);

        /* Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapterSpinner = ArrayAdapter.createFromResource(this, R.array.mnth_array, android.R.layout.simple_spinner_item);
        adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapterSpinner);*/
        // Thong bao ngay trong tuan       
        	
        	Get_SetText_total(array_MAP);
        	list=(ListView)findViewById(R.id.list_score);         	
            // call constructor method of adapter
	        adapter =new ArrayAdapterScore(this, array_MAP_full);
	        
        	Log.e(Activity_Score.class.toString(),"Socre 4");

	        list.setAdapter(adapter);     
        	Log.e(Activity_Score.class.toString(),"Socre 5--------------");

        }catch(Exception e){
        	e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity__score, menu);
        return true;
    }  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_back:{
        	finish();  
        	Intent intent = new Intent(getApplicationContext(),Menu_GridView.class);
				startActivity(intent);				
				break;
        	}
        }
        return true;
    }
    /** lang nghe cho viec chon Spinner */
    private OnItemSelectedListener Spinner_ItemSelectedListener = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
        	// khi thay doi staue        	          
            Spinner_semester=spinner.getSelectedItem().toString();
        	Log.e(Activity_Score.class.toString(),"SPinner chose select :" +spinner.getSelectedItem().toString());
        	// set lai custom list   
        	if(Activity_Score.getSemester_Spinner().toString().equals("Tất cả")){
        		SetAdaptet();
        	}else{
        		SetAdaptet_Filter();
        	}
        	
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    };
    static public String getSemester_Spinner(){
    	return Spinner_semester;
    }
    private void SetAdaptet(){
    	// Get Id of list
    	Get_SetText_total(array_MAP);
    	list=(ListView)findViewById(R.id.list_score);  
    	adapter =new ArrayAdapterScore(this, array_MAP_full);
    	list.setAdapter(adapter);
    }
    private void SetAdaptet_Filter(){
    	// Get Id of list
    	ArrayList<HashMap<String, String>> array_Map_filter=new ArrayList<HashMap<String, String>>();
    	HashMap<String, String> Map;
    	for (int i = 0; i < array_MAP_full.size(); i++) {
    		Map=array_MAP_full.get(i);
    		if(getSemester_Spinner().split("_")[0].toString().equals(Map.get(JsonParser_ScoreStudent.KEY_semesterYear))&&(getSemester_Spinner().split("_")[1].toString().equals(Map.get(JsonParser_ScoreStudent.KEY_semesterIndex)))){
    			array_Map_filter.add(Map);
        	}
		}    	    	
    	Get_SetText_total(array_Map_filter);
    	adapter =new ArrayAdapterScore(this, array_Map_filter);
    	list.setAdapter(adapter);
    }
    private void Get_SetText_total(ArrayList<HashMap<String, String>> array_MAP){
    	Log.e(Activity_Score.class.toString(),"vao  Get_SetText_total");
    	Total_TCC =0.00f;
    	Total_score =0.00f;
    	Total_TCC_full = 0.00f;
    	HashMap<String, String> MAP;
    	Log.e(Activity_Score.class.toString(),"1");
    	for (int i = 0; i < array_MAP.size(); i++) {
			MAP = array_MAP.get(i);
			Log.e(Activity_Score.class.toString(),"2: "+i);
			if(!MAP.get(JsonParser_ScoreStudent.KEY_scoreFinal).toString().equals("") && !MAP.get(JsonParser_ScoreStudent.KEY_subjectCredit).toString().equals("")){
	        	Total_TCC =  Total_TCC+ Float.parseFloat(MAP.get(JsonParser_ScoreStudent.KEY_subjectCredit).toString());
	        	Log.e(Activity_Score.class.toString(),"2.1: "+i);
				Total_score = Total_score + Float.parseFloat(MAP.get(JsonParser_ScoreStudent.KEY_scoreFinal).toString())*Float.parseFloat(MAP.get(JsonParser_ScoreStudent.KEY_subjectCredit).toString());
	        	Log.e(Activity_Score.class.toString(),"2.2: "+i);
			}
        	Total_TCC_full =  Total_TCC_full+ Float.parseFloat(MAP.get(JsonParser_ScoreStudent.KEY_subjectCredit).toString());
        	Log.e(Activity_Score.class.toString(),"i :" +i);
        	Log.e(Activity_Score.class.toString(),"TC  " +Total_TCC);
        	Log.e(Activity_Score.class.toString(),"Diem " +Total_score);
        	Log.e(Activity_Score.class.toString(),"3: "+i);
		} 
    	Log.e(Activity_Score.class.toString(),"4");
    	float TB = 0.00f;
    	Log.e(Activity_Score.class.toString(),"5");
    	if(Total_TCC >0){
    	TB = (float)Total_score /(float)Total_TCC;
    	}else
    	TB=0;
    	Log.e(Activity_Score.class.toString(),"6");
    	TextView TextView_Total_TC = (TextView) findViewById(R.id.Total_TC);
    	Log.e(Activity_Score.class.toString(),"7");
    	TextView_Total_TC.setText(String.format("%.0f",Total_TCC)+"/"+String.format("%.0f",Total_TCC_full));
    	Log.e(Activity_Score.class.toString(),"8");
    	TextView TextView_Total_score = (TextView) findViewById(R.id.Total_score);
    	Log.e(Activity_Score.class.toString(),"9");
    	TextView_Total_score.setText(String.format("%.2f", TB));
    	Log.e(Activity_Score.class.toString(),"10");
    }
}

